package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    /**
     * 根据用户名查用户信息
     * @param username
     * @return
     */
    @Override
    public User findUserByName(String username) {
        //查询用户
        User user = userDao.findUserByName(username);
        if (user == null) {
            return null;
        }
        //查询roles属性
        Set<Role> roleSet = roleDao.findRolesByUserId(user.getId());
        //查询permissions属性
        if (roleSet != null && roleSet.size()>0) {
            //封装permissions属性
            for (Role role : roleSet) {
                Set<Permission> permissionSet = permissionDao.findPermissionByRoleId(role.getId());
                role.setPermissions(permissionSet);
            }
            //封装roles属性
            user.setRoles(roleSet);
        }
        return user;
    }
}
